package com.infoland.afanmall.model.fuPin.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.SizeUtils;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.util.CONST;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.StatusBarKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.util.layoutManager.GridSpacingItemDecoration;
import com.infoland.afanmall.base.util.okhttp.OkHttpKit;
import com.infoland.afanmall.model.fuPin.adapter.FpCityActivityAdapter;
import com.infoland.afanmall.model.fuPin.entity.FuPinGuanPageResponse;
import com.wang.avi.AVLoadingIndicatorView;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.vondear.rxtools.RxTool.getContext;

/**
 * @author 王永吉
 * @date 2018/7/3 11:25
 * 切换省份
 */
public class FpCityListActivity extends AppCompatActivity {
    @BindView(R.id.fp_city_recycle)
    RecyclerView fpCityRecycle;
    @BindView(R.id.fp_include_city_title)
    LinearLayout fpIncludeCityTitle;
    @BindView(R.id.quxiao)
    TextView quxiao;
    @BindView(R.id.avi_pt)
    AVLoadingIndicatorView aviPt;
    FpCityActivityAdapter fpCityActivityAdapter;

    private void initData() {
        refLoadStart();
        getBanner();
    }

    /*** 查询轮播**/
    private void getBanner() {

        new OkHttpKit().send(
                response200 -> {
                    LogKit.E("查询轮播 ---->   ", "  " + response200);
                    setFuPinBanner(JSONObject.parseObject(response200, FuPinGuanPageResponse.class));
                    refLoadOk();
                },
                error -> {
                    refLoadError();
                }
        ).post(CONST.FU_PIN_GUAN, new JSONObject(), this);
    }

    /*** 设置扶贫管轮播图**/
    private void setFuPinBanner(FuPinGuanPageResponse fuPinGuanPageResponse) {
        fpCityActivityAdapter = new FpCityActivityAdapter(R.layout.fu_pin_city_item, fuPinGuanPageResponse.getObject());
        fpCityActivityAdapter.setOnItemClickListener((adapter, view, position) -> {
            FuPinGuanPageResponse.ObjectBean o = (FuPinGuanPageResponse.ObjectBean) adapter.getData().get(position);
            String city_name = o.getProvince_name();
            int city_id = o.getId();
            Bundle bundle = new Bundle();
            LogKit.E("aaaa ---->   ", "  " + city_id);
            LogKit.E("aaaa ---->   ", "  " + city_name);

            bundle.putInt("city_id", city_id);
            bundle.putString("city_name", city_name);
            ToolsKit.returnResultToActivity(this, bundle, CONST.RESULT_CODE_TO_FU_PIN_CITY);
            finish();
        });
        fpCityRecycle.setAdapter(fpCityActivityAdapter);
    }

    /*** 加载或刷新失败**/
    private void refLoadError() {
        aviPt.setVisibility(View.GONE);
    }

    /*** 加载或刷新完成**/
    private void refLoadOk() {
        aviPt.setVisibility(View.GONE);
    }

    /*** 加载或刷新开始**/
    private void refLoadStart() {
        aviPt.setVisibility(View.VISIBLE);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fu_pin_city);
        ButterKnife.bind(this);
        StatusBarKit.immersiveBlackTitleText(this, fpIncludeCityTitle);//沉浸黑字
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 3);
        fpCityRecycle.setLayoutManager(gridLayoutManager);
        fpCityRecycle.addItemDecoration(new GridSpacingItemDecoration(
                3, SizeUtils.dp2px(12),
                true));
        quxiao.setOnClickListener(v -> {
            finish();
        });
        initData();
    }

    @Override
    protected void onDestroy() {
        ToolsKit.deleteOKHttp(this);
        super.onDestroy();

    }
}
